package oa.hleast.xswl.controller.admin.appsicon;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.pojo.domain.SysDictItem;
import oa.hleast.xswl.pojo.form.appsicon.CmsAppsIconForm;
import oa.hleast.xswl.pojo.vo.appsicon.CmsAppsIconVo;
import oa.hleast.xswl.service.ISysCommonConfigService;
import oa.hleast.xswl.service.ISysDictItemService;
import oa.hleast.xswl.service.ISysDictService;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.appsicon.IAppsIconService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * @author 302
 * @description
 * @date 2021/3/10
 */
@Api(tags = "【系统管理】应用图标管理")
@RestController("AppsIconController")
@RequestMapping("/api.admin/v1/appsIcon")
@AllArgsConstructor
@Slf4j
public class AppsIconController {

    private final IAppsIconService appsIconService;
    private final IUmsUserService umsUserService;
    private final ISysDictService dictService;
    private final ISysCommonConfigService commonConfigService;
    private final ISysDictItemService dictItemService;

    @ApiOperation(value = "应用图标位置类型", httpMethod = "GET")
    @GetMapping("/findAppsIconType")
    public Result<List<SysDictItem>> findAppsIconType() {

        List<SysDictItem> result = appsIconService.findAppsIconType();

        return Result.success(result);
    }

    @ApiOperation(value = "新增应用图标", httpMethod = "POST")
    @ApiImplicitParam(name = "appsIconForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsAppsIconForm",dataTypeClass = CmsAppsIconForm.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody CmsAppsIconForm appsIconForm) {
        boolean status = appsIconService.add(appsIconForm);
        return Result.judge(status);
    }

    @ApiOperation(value = "修改应用图标", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "appsIconForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsAppsIconForm",dataTypeClass = CmsAppsIconForm.class)
    })
    @PutMapping(value = "update/{id}")
    public Result<Boolean> update(
            @PathVariable @NonNull Integer id,
            @RequestBody @NonNull CmsAppsIconForm appsIconForm) {

        if ((int) id != appsIconForm.getId()) {
            throw new BizException("数据ID不一致");
        }
        boolean status = appsIconService.update(appsIconForm);
        return Result.judge(status);
    }

    @ApiOperation(value = "应用图标列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 page分页查询", required = true, paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("query-list")
    public Result<List<CmsAppsIconVo>> list(
            @RequestParam @NotBlank String queryMode,
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit,
            @RequestParam @NonNull String appTypeKey
    ) {

        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        Page<CmsAppsIconVo> result = null;
        switch (queryModeEnum) {
            case PAGE:
                log.info("appTypeKey = {}",appTypeKey);
                result = appsIconService.pageListByApps(page, limit, appTypeKey);
                return Result.success(result.getRecords(), result.getTotal());
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "应用图标详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("get-details/{id}")
    public Result<CmsAppsIconVo> detail(@PathVariable Integer id) {
        CmsAppsIconVo advert = appsIconService.detail(id);
        return Result.success(advert);
    }

    @ApiOperation(value = "删除应用图标", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("delete/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") String ids) {
        boolean status = appsIconService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

}
